<?php
namespace app\modules\admin\controllers;
use app\modules\admin\models\Finance;
use app\modules\admin\models\Tips;
use Yii;
use yii\web\Controller;
use app\models\NormalUser;
use app\models\Enterprise;
use app\models\Feedback;
use app\models\Job;
use app\modules\admin\models\Message;
use app\modules\admin\models\Config;
use app\modules\admin\models\Banner;
use app\models\Upload;
use yii\web\UploadedFile;
use yii\data\Pagination;

class IndexController extends Controller
{
    public $defaultAction='index';
    public $type;

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $finance = Finance::find()->where('is_pay=0')->all();
        $job = Job::find()->where('is_examine=0')->all();
        $view = YII::$app->view;
        $view->params['finance'] = count($finance);
        $view->params['job'] = count($job);
    }

    public function isLogin(){
        //判断是否登陆
        $session = Yii::$app->session;
        if(!$session['Admin']['token'])
            return $this->redirect(['login/login']);
    }

    public function serverInfo(){
        //返回服务器统计信息
        exec('top -b -n 1 -d 3', $out);
        $cpu = explode(",", str_replace(" ", "", $out[2]));
        $cpu = (float)$cpu[3];//cpu占比
        $mem = explode(",", str_replace(" ", "", $out[3]));
        $mem_head = explode(":", $mem[0]);
        $mem_total = (int)($mem_head[1] / 1024);//总内存
        $mem_free = (int)($mem[1] / (1024));//空闲内存
        $disk_free = intval(diskfreespace(".") / (1024 * 1024));//空闲磁盘
        $disk_total = intval(disk_total_space(".") / (1024 * 1024));//总磁盘大小
        $data = array($cpu, $mem_total, $mem_free, $disk_total, $disk_free);
        return $data;
    }

    public function actionIndex(){
        //后台首页
        $session = Yii::$app->session;
            $this->isLogin();
            $this->layout = 'index';
            $server_data = $this->serverInfo();
            return $this->render('index', ['data' => $server_data]);
    }

    public function actionUserManage(){
        //普通用户管理
        $this->isLogin();
        $this->layout = 'index';
        $this->type = 'widgets';
        $NormalUser = NormalUser::find();
        $countQuery = clone $NormalUser;
        $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '10']);
        $model = $NormalUser->offset($page->offset)->limit($page->limit)->all();
        return $this->render('userManage', ['page'=>$page,'model'=>$model]);
    }

    public function actionEnterpriseManage(){
        //企业用户管理
        $this->isLogin();
        $this->layout = 'index';
        $Enterprise = Enterprise::find();
        $countQuery = clone $Enterprise;
        $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '10']);
        $model = $Enterprise->offset($page->offset)->limit($page->limit)->all();
        $this->type = 'widgets';
        return $this->render('enterpriseManage', ['model'=>$model,'page'=>$page]);
    }

    public function actionTips(){
        //公告设置
        $this->isLogin();
        $this->layout='index';
        $tips = Tips::find();
        $countQuery = clone $tips;
        $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' =>'10']);
        $model = $tips->offset($page->offset)->limit($page->limit)->all();
        $this->type = 'widgets';
        return $this->render('tipsList',['model'=>$model,'page'=>$page]);
    }

    public function actionTipsAdd(){
        //添加公告
        $this->isLogin();
        $this->layout='index';
        $tips = new Tips();
        if(Yii::$app->request->isPost){
            $session = Yii::$app->session;
            $data = Yii::$app->request->post();
            $data['Tips']['author'] = $session['Admin']['name'];
            $data['Tips']['times'] = time();
            $tips->setAttributes($data['Tips'],false);
            $tips->save(false);
            $this->redirect(['index/tips']);
        }
        $this->type = 'widgets';
        return $this->render('tipsAdd',['model'=>$tips]);
    }

    public function actionTipsModify(){
        //修改公告
        $this->isLogin();
        $this->layout='index';
        $id = Yii::$app->request->get('id');
        if(Yii::$app->request->isPost){
            //修改公告
            $session = Yii::$app->session;
            $post = Yii::$app->request->post('Tips');
            $post['author'] = $session['Admin']['name'];
            $post['times'] = time();
            $tips = Tips::find()->where(['id'=>$id])->one();
            $tips->updateAttributes($post,false);
            echo "<script>alert('修改公告成功！');location.href='index.php?r=admin/index/tips'</script>";
        }else {
            //显示公告
            $tips = Tips::find()->where(['id' => $id])->one();
            $data = array(
                'tips' => $tips,
            );
            $this->type = 'widgets';
            return $this->render('tipsModify', $data);
        }
    }

    public function actionFeedback(){
        // 显示用户意见反馈
        $this->isLogin();
        $this->layout = 'index';
        $Feedback = Feedback::find();
        $countQuery = clone $Feedback;
        $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' =>'10']);
        $model = $Feedback->offset($page->offset)->limit($page->limit)->all();
        $this->type = 'widgets';
        return $this->render('feedback',['model'=>$model,'page'=>$page]);
}
    public function actionDisplay(){
        // 显示用户反馈的意见 后期禁用企业或用户
        // kind = 0 操作用户意见反馈
        $data = Yii::$app->request;
        $kind = Yii::$app->request->get('kind');
        $id = Yii::$app->request->get('id');
        if($data->isGet and $kind==0){
            $Feedbakck = Feedback::find()->where(['id'=>$id])->one();
            $Feedbakck->status = 1;
            $Feedbakck->save(false);
            echo "<script>alert('意见将在前台显示！');location.href='index.php?r=admin/index/feedback'</script>";
        }

        else
            echo"<script>alert('操作有误，请重试！');location.href='index.php?r=admin/index/feedback'</script>";
    }

    public function actionDelete()
    {
        //用于管理员 后台 删除用户和企业、用户反馈的意见、删除公告
        //kind=1 删除用户 kind=0 删除企业 kind=2 删除用户意见 kind=3 删除公告 ，kind=4 删除工作 kind = 5将订单作废
        //kind=6 删除前台轮播
        $data = Yii::$app->request;
        $kind = Yii::$app->request->get('kind');
        $id = Yii::$app->request->get('id');
        if($data->isGet and $kind==0){
            //删除企业
            $enterprise = Enterprise::find()->where(['id'=>$id])->one();
            if($enterprise){
                $enterprise->delete();
                echo "<script>alert('删除企业成功！');location.href='index.php?r=admin/index/enterprise-manage'</script>";
            }else
                echo "<script>alert('删除企业失败！');location.href='index.php?r=admin/index/enterprise-manage'</script>";
        }
        elseif($data->isGet and $kind==1){
            //删除用户
            $user = NormalUser::find()->where(['id' => $id])->one();
            if($user){
                $user->delete();
                echo "<script>alert('删除用户成功！');location.href='index.php?r=admin/index/user-manage'</script>";
            }else
                echo "<script>alert('删除用户失败！');location.href='index.php?r=admin/index/user-manage'</script>";
        }
        elseif($data->isGet and $kind==2){
            //删除用户反馈的意见
            $Feedback = Feedback::find()->where(['id'=>$id])->one();
            if($Feedback){
                $Feedback->delete();
                echo '<script>alert("删除意见成功");location.href="index.php?r=admin/index/feedback";</script>';
            }
        }
        elseif($data->isGet and $kind==3){
            //删除公告
            $tips = Tips::find()->where(['id'=>$id])->one();
            $tips->delete();
            echo "<script>alert('删除公告成功！');location.href='index.php?r=admin/index/tips';</script>";
        }
        elseif($data->isGet and $kind==4){
            $job = Job::find()->where(['id'=>$id])->one();
            $job->delete();
            echo "<script>alert('删除工作成功！');location.href='index.php?r=admin/index/job-list';</script>";
        }
        elseif($data->isGet and $kind==5){
            //将订单作废
            $finance = Finance::find()->where(['id'=>$id])->one();
            $finance->status = 0;
            $finance->save(false);
            echo "<script>alert('已作废，数据将不再显示！');location.href='index.php?r=admin/index/finance-list'</script>";
        }
        elseif($data->isGet and $kind==6){
            //删除轮播
            $banner = Banner::find()->where(['id'=>$id])->one();
            $banner->delete();
            echo "<script>alert('删除成功！');location.href='index.php?r=admin/index/banner-list'</script>";
        }
        else
            echo"<script>alert('数据有误，请重试！');location.href='index.php?r=admin/index'</script>";
    }

    public function actionConfig(){
        //网站信息配置
        $this->isLogin();
        $this->layout='index';
        $data = Yii::$app->request;
        if($data->isPost){
            $config = Config::findOne(1);
            $config->updateAttributes($data->post('Config'));
            echo "<script>alert('修改成功！')</script>";
        }
        $this->type = 'widgets';
        $model = Config::findOne(1);
        if(is_null($model)){
            $model = new Config();
        }
        return $this->render('config',['model'=>$model]);
    }

    public function actionAdd(){
        //  添加企业用户
        $this->isLogin();
        $this->layout='index';
        $model = new Enterprise();
        if(Yii::$app->request->isPost){
            //新增企业用户
            $data = Yii::$app->request->post();
            $Filemodel = $model->photo = UploadedFile::getInstance($model, 'photo');
            $savePath = 'upload/'.time().'.'.$Filemodel->extension;
            $Filemodel->saveAs($savePath);
            $model->setAttributes($data['Enterprise'],false);
            $model->photo = $savePath;
            $model->save(false);
            $this->redirect(['index/enterprise-manage']);
        }
        $this->type = 'widgets';
        return $this->render('add',['model'=>$model]);
    }

    public function actionModify(){
        //  修改企业信息 kind=1
        //  用户信息自行填写，admin不予权限修改
        $this->isLogin();
        $this->layout='index';
        $this->type = 'widgets';
        $model = new Enterprise();
        $kind = Yii::$app->request->get('kind');
        $id = Yii::$app->request->get('id');
        if(Yii::$app->request->isPost){
            //修改企业用户信息
            $data = Yii::$app->request->post();
            //$model->setAttributes($data['Enterprise'],false);
            $model = Enterprise::findOne($id);
            $photo = $model->photo = UploadedFile::getInstances($model, 'photo');
            $Filemodel = $photo[0];
            $savePath = 'upload/'.time().'.'.$Filemodel->extension;
            $Filemodel->saveAs($savePath);
            $model->updateAttributes($data['Enterprise']);
            $model->photo = $savePath;
            $model->save(false);

            $this->redirect(['index/enterprise-manage']);
        }elseif(Yii::$app->request->isGet and $kind==1){
            //显示数据
            $Enterprise = Enterprise::find()->where(['id'=>$id])->one();
            $data=array(
                'Enterprise'=>$Enterprise
            );
            return $this->render('modify',$data);
        }elseif(Yii::$app->request->isGet){
            echo"<script>alert('用户信息没有权限修改！');location.href='index.php?r=admin/index/user-manage';</script>";
        }else
            return $this->render('add',['model'=>$model]);
    }

    public function actionJobAdd(){
        //添加工作
        $this->isLogin();
        $this->layout='index';
        $job = new Job();
        if(Yii::$app->request->isPost){
            //新增职位
            $post = Yii::$app->request->post('Job');
            $post['is_examine'] = 1;
            $job->setAttributes($post,false);
            $job->save(false);
            echo "<script>alert('发布成功！');location.href='index.php?r=admin/index/job-list'</script>";
        }
        $this->type = 'widgets';
        return $this->render('jobAdd',['job'=>$job]);
    }

    public function actionJobModify(){
        // 修改工作信息
        $this->isLogin();
        $this->layout = 'index';
        $id = Yii::$app->request->get('id');
        if(Yii::$app->request->isPost){
            //修改工作信息
            $post = Yii::$app->request->post('Job');
            $job = Job::find()->where(['id'=>$id])->one();
            $job->updateAttributes($post,false);
            echo "<script>alert('修改工作信息成功！');location.href='index.php?r=admin/index/job-list'</script>";
        }else {
            //显示工作信息
            $job = Job::find()->where(['id' => $id])->one();
            $this->type = 'widgets';
            return $this->render('jobModify', ['job'=>$job]);
        }
}

    public function actionJobList(){
        //显示工作列表
        $this->isLogin();
        $this->layout='index';
        $job = Job::find()->where('is_examine=1');
        $countQuery = clone $job;
        $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' =>'10']);
        $model = $job->offset($page->offset)->limit($page->limit)->all();
        $this->type = 'widgets';
        return $this->render('jobList',['model'=>$model,'page'=>$page]);
    }

    public function actionJobExamine()
    {
        //显示工作待审核列表
        $this->isLogin();
        $this->layout = 'index';
        $this->type = 'widgets';
        $kind =  Yii::$app->request->get('kind');
        if(!is_null($kind)&&$kind==1){
            //通过请求
            $id = Yii::$app->request->get('id');
            $model = Job::find()->where(['id'=>$id])->one();
            $model->is_examine=1;
            $model->update(false);
            echo "<script>alert('通过成功！');location.href='index.php?r=admin/index/job-list'</script>";
        }
        if(!is_null($kind)&&$kind==0){
            //拒绝请求
            $id = Yii::$app->request->get('id');
            $model = Job::find()->where(['id'=>$id])->one();
            $model->delete();
            echo "<script>alert('拒绝成功！');location.href='index.php?r=admin/index/job-list'</script>";
        }
        $job = Job::find()->where('is_examine=0')->all();
        return $this->render('jobExamine', ['job' => $job]);
    }
    public function actionJobMatch(){
        // kind=1 匹配工作 kind=0 取消匹配
        //匹配后 填写匹配信息 并将is_match 置为1
        //取消匹配 填写信息 并将is_match 置为0
        $this->isLogin();
        $this->layout = 'index';
        $kind = Yii::$app->request->get('kind');
        $id = Yii::$app->request->get('id');
        $job = Job::find()->where(['id'=>$id])->one();
        if(Yii::$app->request->isPost and $kind==1){
            //匹配信息将is_match 置为1
            $post = Yii::$app->request->post('Job');
            $job->is_match = 1;
            $job->uid = $post['uid'];
            $job->message = $post['message'];
            $job->save(false);
            echo "<script>alert('匹配成功！');location.href='index.php?r=admin/index/job-list'</script>";
        }
        if(Yii::$app->request->isPost and $kind==0){
            //取消匹配将is_match 置为0
            $post = Yii::$app->request->post('Job');
            $job->is_match = 0;
            $job->uid = 0;
            $job->message = $post['message'];
            $job->save(false);
            echo "<script>alert('取消成功！');location.href='index.php?r=admin/index/job-list'</script>";
        }
        return $this->render('jobMatch',['job'=>$job]);
    }

    public function actionSendMessage(){
        // 给用户发送消息
        $this->isLogin();
        $this->layout='index';
        $message =  new Message();
        $id = Yii::$app->request->get('id');
        if(Yii::$app->request->isPost)
        {
            $post = Yii::$app->request->post('Message');
            $message->uid = $id;
            $message->times = time();
            $message->setAttributes($post,false);
            $message->save(false);
            echo "<script>alert('信息发送成功！');location.href='index.php?r=admin/index/user-manage'</script>";
        }
        $this->type = 'widgets';
        return $this->render('sendMessage',['message'=>$message]);
    }

    public function actionFinanceList(){
        $this->isLogin();
        $this->layout='index';
        $finance = Finance::find()->where(['status'=>1]);
        $countQuery = clone $finance;
        $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' =>'10']);
        $model = $finance->offset($page->offset)->limit($page->limit)->all();
        $this->type = 'widgets';
        return $this->render('financeList',['model'=>$model,'page'=>$page]);
    }

    public function actionFinanceDeal(){
        $this->isLogin();
        $this->layout='index';
        $finance = Finance::find()->where('is_pay=0');
        $countQuery = clone $finance;
        $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' =>'10']);
        $model = $finance->offset($page->offset)->limit($page->limit)->all();
        $this->type = 'widgets';
        return $this->render('financeDeal',['model'=>$model,'page'=>$page]);
    }

    public function actionFinanceModify(){
        $this->isLogin();
        $this->layout='index';
        $this->type = 'widgets';
        if(Yii::$app->request->isPost){
            $post = Yii::$app->request->post('Finance');
            $model = Finance::find()->where(['id'=>$post['id']])->one();
            $post['is_pay']=1;
            $post['complete_time']=time();
            $model->updateAttributes($post,false);
            echo "<script>alert('录入成功！');location.href='index.php?r=admin/index/finance-deal'</script>";
        }
        if(Yii::$app->request->get('id')){
            $model = Finance::find()->where(['id'=>Yii::$app->request->get('id')])->one();
            return $this->render('financeModify',['finance'=>$model]);
        }else{
            echo "<script>alert('信息载入失败，请重试！');location.href='index.php?r=admin/index/finance-deal'</script>";
        }

    }

    //Banner 前台首页轮播内容发表、删除、等操作
    public function actionBannerList(){
        $this->layout = 'index';
        $model = Banner::find()->all();
        return $this->render('bannerList',['model'=>$model]);
    }

    public function actionBannerAdd(){
        $this->layout = 'index';
        $model = new Banner;
        $model->scenario = 'bannerAdd';
        if(Yii::$app->request->isPost){
            //新增轮播
            $data = Yii::$app->request->post();
            $data['Banner']['times'] = time();
            $Filemodel = $model->img = UploadedFile::getInstance($model, 'img');
            $savePath = 'upload/'.time().'.'.$Filemodel->extension;
            $Filemodel->saveAs($savePath);
            $model->setAttributes($data['Banner'],false);
            $model->img = $savePath;
            $model->save(false);
            $this->redirect(['index/banner-list']);
        }
        return $this->render('bannerAdd',['model'=>$model]);
    }

    public function actionBannerModify(){
        //修改轮播内容
        $this->layout = 'index';
        $id = Yii::$app->request->get('id');
        $model = Banner::find()->where(['id'=>$id])->one();
        $model->scenario = 'bannerModify';
        if(Yii::$app->request->isPost){
            $data = Yii::$app->request->post();
            $data['Banner']['times'] = time();
            $model = Banner::findOne($id);
            $img = $model->img = UploadedFile::getInstances($model, 'img');
            $Filemodel = $img[0];
            $savePath = 'upload/'.time().'.'.$Filemodel->extension;
            $Filemodel->saveAs($savePath);
            $model->updateAttributes($data['Banner']);
            $model->img = $savePath;
            $model->save(false);
            $this->redirect(['index/banner-list']);
        }
        return $this->render('bannerModify',['model'=>$model]);
    }

    public function actionBannerShow(){
        //kind=0 隐藏该轮播内容
        //kind=1 显示该轮播内容
        $id = Yii::$app->request->get('id');
        $kind = Yii::$app->request->get('kind');
        $model = Banner::find()->where(['id'=>$id])->one();
        if(Yii::$app->request->isGet and $kind==0){
            //隐藏
            $model->status = 0;
            $model->save(false);
            $this->redirect(['index/banner-list']);
        }
        elseif(Yii::$app->request->isGet and $kind==1){
            $model->status = 1;
            $model->save(false);
            $this->redirect(['index/banner-list']);
        }
        else
            echo"<script>alert('操作失败！请重试');location.href='index.php?r=admin/index/banner-list'</script>";
    }


}